package com.bff.gaia.streaming.api.mo.generate;

import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import com.bff.gaia.api.java.utils.ParameterTool;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;

public class NormalDis extends NormalDistribution {
	public NormalDis() {}
	public NormalDis(int mean, int sd) {
		super(mean, sd);
	}
	public double[] sample(int sampleSize) {
		if (sampleSize <= 0)
			throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
				Integer.valueOf(sampleSize));
		double[] out = new double[sampleSize];
		for (int i = 0; i < sampleSize; i++)
			out[i] = sample();//?
		return out;
	}

	public void writeDataToFile(String fileName) throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileName)));
		DecimalFormat decimalFormat = new DecimalFormat("00.00000000000");
		NormalDis dis = new NormalDis(500, 10);
		double[] x = dis.sample(67108864);
		for (int i = 0; i < x.length; i++) {
			writer.write(String.valueOf(decimalFormat.format(x[i])));
			writer.write("\n");
			writer.flush();
		}
		writer.close();
	}

	public static void main(String[] args) {
		ParameterTool parameterTool = ParameterTool.fromArgs(args);
		int size = parameterTool.getInt("size", 10);
		String output = parameterTool.get("output", "./data.txt");
		NormalDis dis = new NormalDis();
		for (int i = 0; i < size; i++) {
			try {
				dis.writeDataToFile(output);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}